package com.jingyanzi.marketing.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.marketing.dao.DiscountMapper;
import com.jingyanzi.marketing.domain.DiscountDo;
import com.jingyanzi.marketing.entity.MaDiscount;
import com.jingyanzi.marketing.entity.MaDiscountExample;
import com.jingyanzi.marketing.entity.MaDiscountExample.Criteria;
import com.jingyanzi.marketing.query.DiscountQuery;
import com.jingyanzi.marketing.result.ResultDO;
import com.jingyanzi.marketing.result.ResultSupport;
import com.jingyanzi.marketing.service.IDiscountService;
import com.jingyanzi.marketing.util.BeanUtilsExtends;
@Service("maDiscountService")
public class DiscountServiceImpl extends BaseServiceImpl implements IDiscountService {

	@Autowired
	private DiscountMapper discountMapper;
	
	public ResultDO add(DiscountDo maDiscountDo) {
		MaDiscount discountDo = new MaDiscount();
		
		ResultSupport result = BeanUtilsExtends.copy(discountDo, maDiscountDo);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
        //role.setCode(UUID.randomUUID().toString());
        discountDo.setCreatetime(Calendar.getInstance().getTime());
        discountDo.setIsDelete(1);
        int r = -1;
        
        try {
            r = discountMapper.insert(discountDo);
        } catch (Exception e) {
        	e.printStackTrace();
    		result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加优惠卷失败");
			result.setSuccess(false);
			return result;
        }
        
        if (r < 1) {
        	result.setSuccess(false);
        }
 
        result.setModel(ResultDO.FIRST_MODEL_KEY, discountDo.getId());
        return result;
	}
	
	public ResultDO modifi(DiscountDo maDiscountDo) {
		MaDiscount discountDo = new MaDiscount();
		
		ResultSupport result = BeanUtilsExtends.copy(discountDo, maDiscountDo);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
        int r = -1;
        
        try {
            r = discountMapper.updateByPrimaryKeySelective(discountDo);
        } catch (Exception e) {
        	e.printStackTrace();
    		result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
    		result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改优惠卷失败");
			result.setSuccess(false);
			return result;
        }
        
        if (r < 1) {
        	result.setSuccess(false);
        }
        return result;
	}

	public ResultDO remove(Integer id) {
		MaDiscount discountDo = new MaDiscount();
		
		ResultSupport result = new ResultSupport();
        if (!result.isSuccess())
        	return result;
        discountDo.setId(id);
        discountDo.setIsDelete(-1);
        int r = -1;
        
        try {
            r = discountMapper.updateByPrimaryKeySelective(discountDo);
        } catch (Exception e) {
        	e.printStackTrace();
       		result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
    		result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除优惠卷失败");
			result.setSuccess(false);
			return result;
        }
        
        if (r < 1) {
     		result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
    		result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除优惠卷失败");
			result.setSuccess(false);
			return result;
        }
        return result;
	}

	public ResultDO get(Integer id) {
		MaDiscount discountDo = new MaDiscount();
		
		ResultSupport result = new ResultSupport();
        if (!result.isSuccess())
        	return result;
        try {
        	discountDo = discountMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
        	e.printStackTrace();
     		result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
    		result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取优惠卷失败");
			result.setSuccess(false);
			return result;
        }
        
        if (discountDo==null) {
        	result.setErrorCode(ResultDO.DISCOUNT_GET_ERROR);
    		result.setErrorMsg("不存在该优惠卷");
			result.setSuccess(false);
			return result;
        }
        DiscountDo maDiscountDo = getMaDiscountDo(discountDo);
        result.setModel(ResultSupport.FIRST_MODEL_KEY, maDiscountDo);
        return result;
	}

	public ResultDO query(DiscountQuery discountQuery) {
		ResultSupport result = new ResultSupport();

		List<MaDiscount> list = null;

		MaDiscountExample example = new MaDiscountExample();
		Criteria createCriteria = example.createCriteria();
		if (StringUtils.isNotBlank(discountQuery.getName())) {
			createCriteria.andNameEqualTo(discountQuery.getName());
		}
		
		if (discountQuery.getType()!=null) {
			createCriteria.andTypeEqualTo(discountQuery.getType());
		}
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if (StringUtils.isNotBlank(discountQuery.getValidity())) {
				createCriteria.andValidityLessThanOrEqualTo(sdf.parse(discountQuery.getValidity()));
			}	
		} catch (Exception e) {
			e.printStackTrace();
		}			
		
		createCriteria.andIsDeleteEqualTo(1);

		List<DiscountDo> discountDos = null;

		if (discountQuery.isPage()) {
			example.setLimitByPage(discountQuery.getStart() + "," + discountQuery.getPageRows());
			long count = discountMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = discountMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
     		result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
    		result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取优惠卷失败");
			result.setSuccess(false);
			return result;
		}

		discountDos = getMaDiscountDoList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, discountDos);

		return result;
	}

}
